﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

public partial class reports_Transaction_rptPrintInvoice1 : System.Web.UI.Page
{
    private recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
    private string parameters = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime? dPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["periodeAwal"], Helper.Culture.Indonesian);
            DateTime? dPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["periodeAkhir"], Helper.Culture.Indonesian);
            string debtorID = Context.Request.Params["debtorID"] == null ? string.Empty : Context.Request.Params["debtorID"];

            //konversi tanggal
            string periodeAwal = string.Format("{0:yyyyMMdd}", dPeriodeAwal);
            string periodeAkhir = string.Format("{0:yyyyMMdd}", dPeriodeAkhir);

            // filter debtorID
            if (!debtorID.Equals(string.Empty))
            {
                parameters += "where recDebtor.DebtorID = ISNULL ('" + debtorID + "', recDebtor.DebtorID)";
            }

            //filter invoice date
            if (!debtorID.Equals(string.Empty))
            {
                if (!periodeAwal.Equals(string.Empty) && !periodeAkhir.Equals(string.Empty))
                    parameters += " AND recInvoice.InvoiceDate BETWEEN ISNULL ('" + periodeAwal + "', recInvoice.InvoiceDate) AND ISNULL ('" + periodeAkhir + "', recInvoice.InvoiceDate)";
            }
            else if (!periodeAwal.Equals(string.Empty) && !periodeAkhir.Equals(string.Empty))
            {
                    parameters += "WHERE recInvoice.InvoiceDate BETWEEN ISNULL ('" + periodeAwal + "', recInvoice.InvoiceDate) AND ISNULL ('" + periodeAkhir + "', recInvoice.InvoiceDate)";
            }

            try
            {
                DataTable dt = recInvoiceDal.GetListReport(parameters);

                rsViewer.Reset();
                rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptInvoice1.rdlc");
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "DataSet1";
                rds.Value = dt;

                rsViewer.LocalReport.DataSources.Clear();
                rsViewer.LocalReport.DataSources.Add(rds);
                rsViewer.LocalReport.Refresh();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}